﻿using System;
using System.Threading;
using ZilLion.Core.Log;

namespace ZilLion.Core.TaskManager.Client.SystemMonitor
{
    /// <summary>
    ///     基础监控者
    /// </summary>
    public abstract class BaseMonitor
    {
        protected Thread Thread;

        public BaseMonitor()
        {
            Thread = new Thread(TryRun) {IsBackground = true};
            Thread.Start();
        }

        /// <summary>
        ///     监控间隔时间 （毫秒）
        /// </summary>
        public virtual int Interval { get; set; }

        private void TryRun()
        {
            while (true)
                try
                {
                    Run();
                    Thread.Sleep(Interval);
                }
                catch (Exception exp)
                {
                    ZilLionLogManager.GetLogger().Error(ex: exp, tags: GetType().Name + "监控严重错误");
                }
        }

        /// <summary>
        ///     监控执行方法约定
        /// </summary>
        protected virtual void Run()
        {
        }
    }
}